home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / PCX_LIB.ARJ / PCX_EXAM.C < prev    next >
C/C++ Source or Header  |  1991-04-03  |  10KB  |  340 lines

  1. /*
  2.  *************************************************************************
  3.  *
  4.  *  PCX_EXAM.C - PCX_LIB File Header Examination Utility
  5.  *
  6.  *  Version:    1.00B
  7.  *
  8.  *  History:    91/02/14 - Created
  9.  *              91/04/01 - Release 1.00A
  10.  *              91/04/06 - Release 1.00B
  11.  *
  12.  *  Compiler:   Microsoft C V6.0
  13.  *
  14.  *  Author:     Ian Ashdown, P.Eng.
  15.  *              byHeart Software
  16.  *              620 Ballantree Road
  17.  *              West Vancouver, B.C.
  18.  *              Canada V7S 1W3
  19.  *              Tel. (604) 922-6148
  20.  *              Fax. (604) 987-7621
  21.  *
  22.  *  Copyright:  Public Domain
  23.  *
  24.  *************************************************************************
  25.  */
  26.  
  27. /*
  28.  *************************************************************************
  29.  *
  30.  *  PORTABILITY NOTES
  31.  *
  32.  *  1.  When porting this program to other processors, remember that words
  33.  *      are stored by 80x86-based machines in the big-endian format.  That
  34.  *      is, the eight least significant bits (lower byte) are stored
  35.  *      first, followed by the eight most significant bits (upper byte).
  36.  *      If PCX-format files are transferred to little-endian machines
  37.  *      (such as those based on 680x0 and Z8000 processors), the order of
  38.  *      bytes within each word will have to be reversed before they can 
  39.  *      be interpreted.  (This applies to the file header only, since the
  40.  *      encoded image data and optional 256-color palette are stored as
  41.  *      bytes.)
  42.  *
  43.  ************************************************************************* 
  44.  */
  45.  
  46. /*      INCLUDE FILES                                                   */
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include "pcx_int.h"
  51.  
  52. /*      FORWARD REFERENCES                                              */
  53.  
  54. /*      GLOBALS                                                         */
  55.  
  56. /*      PUBLIC FUNCTIONS                                                */
  57.  
  58. /*
  59.  *************************************************************************
  60.  *
  61.  *  MAIN - Executive Function
  62.  *
  63.  *  Purpose:    To read and display a PCX-format image file header.
  64.  *
  65.  *  Setup:      int main
  66.  *              (
  67.  *                int argc,
  68.  *                char **argv
  69.  *              )
  70.  *
  71.  *  Where:      argc is the number of command-line arguments.
  72.  *              argv is a pointer to an array of command-line argument
  73.  *                strings.
  74.  *
  75.  *  Return:     0 if successful; otherwise 2.
  76.  *
  77.  *  Result:     The file header information is written to "stdout".
  78.  *
  79.  *  Note:       Usage is:
  80.  *
  81.  *                PCX_EXAM filename
  82.  *
  83.  *              where "filename" is the name of a PCX-format image file.
  84.  *
  85.  *************************************************************************
  86.  */
  87.  
  88. int main
  89. (
  90.   int argc,
  91.   char **argv
  92. )
  93. {
  94.   char *vers;                   /* Version number string pointer        */
  95.   char *pal_type;               /* Palette type string pointer          */
  96.   int range;                    /* Palette range                        */
  97.   int indicator;                /* Extended palette indicator byte      */
  98.   BOOL status = TRUE;           /* Return status                        */
  99.   BOOL ep_flag = FALSE;         /* Extended palette flag                */
  100.   FILE *fp;                     /* File pointer                         */
  101.   PCX_HDR *hdrp;                /* File header structure pointer        */
  102.   PCX_PAL *ext_palettep;        /* Extended palette pointer             */
  103.  
  104.   /* Display program title                                              */
  105.  
  106.   puts("\nPCX_EXAM - PCX Image File Examination Utility\n");
  107.  
  108.   /* Check for filename                                                 */
  109.  
  110.   if (argc < 2)
  111.   {
  112.     /* Display usage information                                        */
  113.  
  114.     fputs("  Synopsis:  This public domain utility displays inf", stderr);
  115.     fputs("ormation contained in the\n             header of a ", stderr);
  116.     fputs("Paintbrush (R) PCX-format image file.\n\n  Usage:   ", stderr);
  117.     fputs("  PCX_EXAM filename\n\n  Example:   PCX_EXAM picture", stderr);
  118.     fputs(".pcx\n", stderr);
  119.  
  120.     return (2);
  121.   }
  122.  
  123.   /* Allocate a PCX file header buffer                                  */
  124.  
  125.   if ((hdrp = (PCX_HDR *) malloc(sizeof(PCX_HDR))) == (PCX_HDR *) NULL)
  126.   {
  127.     fputs("ERROR: out of memory\n", stderr);
  128.     return (2);
  129.   }
  130.  
  131.   /* Open the PCX image file in binary mode                             */
  132.  
  133.   if ((fp = fopen(argv[1], "rb")) == (FILE *) NULL)
  134.   {
  135.     fprintf(stderr, "ERROR: cannot open file %s\n", argv[1]);
  136.  
  137.     free(hdrp);         /* Free the file header memory                  */
  138.  
  139.     return (2);
  140.   }
  141.  
  142.   /* Read the file header                                               */
  143.  
  144.   if (status == TRUE)
  145.     if (fseek(fp, 0L, SEEK_SET) != 0)
  146.     {
  147.       fprintf(stderr, "ERROR: cannot read file %s\n", argv[1]);
  148.       status = FALSE;
  149.     }
  150.  
  151.   if (status == TRUE)
  152.     if (fread(hdrp, sizeof(PCX_HDR), 1, fp) != 1)
  153.     {
  154.       fprintf(stderr, "ERROR: cannot read file %s\n", argv[1]);
  155.       status = FALSE;
  156.     }
  157.  
  158.   /* Validate the PCX file format                                       */
  159.  
  160.   if (status == TRUE)
  161.     if ((hdrp->pcx_id != 0x0a) || (hdrp->encoding != 1))
  162.     {
  163.       fprintf(stderr, "ERROR: file %s not valid PCX format\n", argv[1]);
  164.       status = FALSE;
  165.     }
  166.  
  167.   /* Determine the version number                                       */
  168.  
  169.   switch (hdrp->version)
  170.   {
  171.     case 0:
  172.  
  173.       vers = "PC Paintbrush 2.5";
  174.  
  175.       break;
  176.  
  177.     case 2:
  178.  
  179.       vers = "PC Paintbrush 2.8 (with palette information)";
  180.  
  181.       break;
  182.  
  183.     case 3:
  184.  
  185.       vers = "PC Paintbrush 2.8 (without palette information)";
  186.  
  187.       break;
  188.  
  189.     case 4:
  190.  
  191.       vers = "PC Paintbrush for Windows (not 3.0)";
  192.  
  193.       break;
  194.  
  195.     case 5:
  196.  
  197.       vers = "PC Paintbrush 3.0 and greater";
  198.  
  199.       break;
  200.  
  201.     default:
  202.  
  203.       vers = "Unknown version";
  204.  
  205.       break;
  206.   }
  207.  
  208.   /* Display the PCX file header information                            */
  209.  
  210.   printf("PCX filename: %s\n", argv[1]);
  211.   printf("Version: %s\n", vers);
  212.   printf("Encoding: %s\n", hdrp->encoding == 1 ? "Run length" :
  213.       "Unknown");
  214.   printf("%d bits per pixel\n", hdrp->bppixel);
  215.   printf("Image from (%d, %d) to (%d, %d) pixels.\n", hdrp->xul,
  216.       hdrp->yul, hdrp->xlr, hdrp->ylr);
  217.   printf("Created on a device with %d x %d dpi resolution\n",
  218.       hdrp->horz_res, hdrp->vert_res);
  219.   printf("Number of color planes: %d\n", hdrp->nplanes);
  220.   printf("Bytes per color plane scan line: %d\n", hdrp->bppscan);
  221.  
  222.   switch (hdrp->palette_type & PCX_PAL_MASK)
  223.   {
  224.     case 1:
  225.  
  226.       pal_type = "color or B&W";
  227.  
  228.       break;
  229.  
  230.     case 2:
  231.  
  232.       pal_type = "grayscale";
  233.  
  234.       break;
  235.  
  236.     default:
  237.  
  238.       pal_type = "unknown";
  239.  
  240.       break;
  241.   }
  242.  
  243.   printf("Palette type: %s\n", pal_type);
  244.  
  245.   /* Check for extended (256-color) palette                             */
  246.  
  247.   if (hdrp->version == 5)
  248.   {
  249.     /* Check for valid palette indicator byte                           */
  250.  
  251.     if (fseek(fp, -769L, SEEK_END) != 0)
  252.     {
  253.       fprintf(stderr, "ERROR: cannot read file %s\n", argv[1]);
  254.       status = FALSE;
  255.     }
  256.  
  257.     if (status == TRUE)
  258.     {
  259.       if ((indicator = getc(fp)) == PCX_EPAL_FLAG)
  260.       {
  261.         /* Allocate an extended palette buffer                          */
  262.  
  263.         if ((ext_palettep = (PCX_PAL *) calloc(sizeof(PCX_PAL),
  264.             PCX_EPAL_SIZE)) == (PCX_PAL *) NULL)
  265.         {
  266.           fputs("ERROR: out of memory\n", stderr);
  267.           status = FALSE;
  268.         }
  269.  
  270.         /* Read the extended palette                                    */
  271.  
  272.         if (status == TRUE)
  273.         {
  274.           if (fread(ext_palettep, sizeof(PCX_PAL), PCX_EPAL_SIZE, fp) !=
  275.               PCX_EPAL_SIZE)
  276.           {
  277.             free(ext_palettep);         /* Free extended palette buffer */
  278.             status = FALSE;
  279.           }
  280.         }
  281.  
  282.         ep_flag = TRUE;         /* Indicate extended palette exists     */
  283.       }
  284.     }
  285.   }
  286.  
  287.   if (status == TRUE)
  288.     if (ep_flag == TRUE)
  289.     {
  290.       /* Display extended (256-color) palette                           */
  291.  
  292.       puts("Extended 256-color palette:\n");
  293.  
  294.       puts("COLOR   RED     GREEN   BLUE\n");
  295.  
  296.       for (range = 0; range < PCX_EPAL_SIZE; range++)
  297.         printf(" %03d      %2x      %2x     %2x\n", range,
  298.             ext_palettep[range].red, ext_palettep[range].green,
  299.             ext_palettep[range].blue);
  300.  
  301.       putchar('\n');
  302.  
  303.       free(ext_palettep);       /* Free the extended palette            */
  304.     }
  305.     else
  306.     {
  307.       /* Display file header palette                                    */
  308.  
  309.       puts("File header color palette:\n");
  310.  
  311.       printf("RED ...   ");
  312.  
  313.       for (range = 0; range < PCX_PAL_SIZE; range++)
  314.         printf("%2x  ", hdrp->palette[range].red);
  315.  
  316.       printf("\nGREEN ... ");
  317.  
  318.       for (range = 0; range < PCX_PAL_SIZE; range++)
  319.         printf("%2x  ", hdrp->palette[range].green);
  320.  
  321.       printf("\nBLUE ...  ");
  322.  
  323.       for (range = 0; range < PCX_PAL_SIZE; range++)
  324.         printf("%2x  ", hdrp->palette[range].blue);
  325.  
  326.       putchar('\n');
  327.     }
  328.  
  329.   if (fclose(fp) == EOF)        /* Close the file                       */
  330.   {
  331.     fprintf(stderr, "Error: cannot close file %s\n", argv[1]);
  332.     status = FALSE;
  333.   }
  334.  
  335.   free (hdrp);          /* Free the file header buffer                  */
  336.  
  337.   return (0);
  338. }
  339.  
  340.